package demo1.Dialogs;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SinDialog extends JDialog {
    JPanel root = new JPanel();
    JTextField textField = new JTextField(10);
    JButton buttonApprove = new JButton("确定");
    JButton buttonCancel = new JButton("取消");
    private boolean isApprove;
    public SinDialog() {
        this.setContentPane(root);
        Init();
    }
    public boolean show(Window owner) {
        this.setTitle("设置为");
        this.setModal(true);
        this.setSize(200,100);
        this.centerInOwner();
        this.setVisible(true);
        return isApprove;
    }
    private void Init() {
        root.add(textField);

        root.add(buttonApprove);
        buttonApprove.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isApprove = true;
                setVisible(false);
            }
        });

        root.add(buttonCancel);
        buttonCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isApprove = false;
                setVisible(false);
            }
        });
    }
    public String getTextStr() {
        return textField.getText();
    }
    private void centerInOwner() {
        Rectangle ownerRect = this.getOwner().getBounds();
        int width = this.getWidth();
        int height = this.getHeight();
        int x = ownerRect.x + (ownerRect.width - width)/2;
        int y = ownerRect.y + (ownerRect.height - height)/2;
        this.setBounds(x,y, width, height);
    }
}
